var express = require('express');
var router = express.Router();
const axios = require("axios")

// 解决跨域
router.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  res.header('Access-Control-Allow-Methods', '*');
  res.header('Content-Type', 'application/json;charset=utf-8');
  next();
})
router.get("/users", function (req, res) {
  const { q } = req.query
  axios({
    url: 'https://api.github.com/search/users',
    params: { q }
  }).then(response => {
    res.json(response.data)
  }).catch(() => {
    res.json({
      items: [
        {
          login: "yyx990803",
          html_url: "https://github.com/yyx990803",
          avatar_url:
            "https://avatars3.githubusercontent.com/u/499550?s=460&u=de41ec9325e8a92e281b96a1514a0fd1cd81ad4a&v=4",
          id: 1,
        },
        {
          login: "ruanyf",
          html_url: "https://github.com/ruanyf",
          avatar_url: "https://avatars2.githubusercontent.com/u/905434?s=460&v=4",
          id: 2,
        },
        {
          login: "yyx9908032",
          html_url: "https://github.com/yyx990803",
          avatar_url:
            "https://avatars3.githubusercontent.com/u/499550?s=460&u=de41ec9325e8a92e281b96a1514a0fd1cd81ad4a&v=4",
          id: 3,
        },
        {
          login: "ruanyf2",
          html_url: "https://github.com/ruanyf",
          avatar_url: "https://avatars2.githubusercontent.com/u/905434?s=460&v=4",
          id: 4,
        },
        {
          login: "yyx9908033",
          html_url: "https://github.com/yyx990803",
          avatar_url:
            "https://avatars3.githubusercontent.com/u/499550?s=460&u=de41ec9325e8a92e281b96a1514a0fd1cd81ad4a&v=4",
          id: 5,
        },
        {
          login: "ruanyf3",
          html_url: "https://github.com/ruanyf",
          avatar_url: "https://avatars2.githubusercontent.com/u/905434?s=460&v=4",
          id: 6,
        },
        {
          login: "yyx9908034",
          html_url: "https://github.com/yyx990803",
          avatar_url:
            "https://avatars3.githubusercontent.com/u/499550?s=460&u=de41ec9325e8a92e281b96a1514a0fd1cd81ad4a&v=4",
          id: 7,
        },
        {
          login: "ruanyf4",
          html_url: "https://github.com/ruanyf",
          avatar_url: "https://avatars2.githubusercontent.com/u/905434?s=460&v=4",
          id: 8,
        },
        {
          login: "yyx9908035",
          html_url: "https://github.com/yyx990803",
          avatar_url:
            "https://avatars3.githubusercontent.com/u/499550?s=460&u=de41ec9325e8a92e281b96a1514a0fd1cd81ad4a&v=4",
          id: 9,
        },
      ],
    })
  })
})

router.get("/users2", function (req, res) {
  res.json({
    items: [
      {
        login: "yyx990803",
        html_url: "https://github.com/yyx990803",
        avatar_url:
          "https://avatars3.githubusercontent.com/u/499550?s=460&u=de41ec9325e8a92e281b96a1514a0fd1cd81ad4a&v=4",
        id: 1,
      },
      {
        login: "ruanyf",
        html_url: "https://github.com/ruanyf",
        avatar_url: "https://avatars2.githubusercontent.com/u/905434?s=460&v=4",
        id: 2,
      },
      {
        login: "yyx9908032",
        html_url: "https://github.com/yyx990803",
        avatar_url:
          "https://avatars3.githubusercontent.com/u/499550?s=460&u=de41ec9325e8a92e281b96a1514a0fd1cd81ad4a&v=4",
        id: 3,
      },
      {
        login: "ruanyf2",
        html_url: "https://github.com/ruanyf",
        avatar_url: "https://avatars2.githubusercontent.com/u/905434?s=460&v=4",
        id: 4,
      },
      {
        login: "yyx9908033",
        html_url: "https://github.com/yyx990803",
        avatar_url:
          "https://avatars3.githubusercontent.com/u/499550?s=460&u=de41ec9325e8a92e281b96a1514a0fd1cd81ad4a&v=4",
        id: 5,
      },
      {
        login: "ruanyf3",
        html_url: "https://github.com/ruanyf",
        avatar_url: "https://avatars2.githubusercontent.com/u/905434?s=460&v=4",
        id: 6,
      },
      {
        login: "yyx9908034",
        html_url: "https://github.com/yyx990803",
        avatar_url:
          "https://avatars3.githubusercontent.com/u/499550?s=460&u=de41ec9325e8a92e281b96a1514a0fd1cd81ad4a&v=4",
        id: 7,
      },
      {
        login: "ruanyf4",
        html_url: "https://github.com/ruanyf",
        avatar_url: "https://avatars2.githubusercontent.com/u/905434?s=460&v=4",
        id: 8,
      },
      {
        login: "yyx9908035",
        html_url: "https://github.com/yyx990803",
        avatar_url:
          "https://avatars3.githubusercontent.com/u/499550?s=460&u=de41ec9325e8a92e281b96a1514a0fd1cd81ad4a&v=4",
        id: 9,
      },
    ],
  })
});

router.use((request, response, next) => {
  console.log('请求来自于', request.get('Host'));
  console.log('请求的地址', request.url);
  next()
})

// router.get('/users', (request, response) => {
//   const users = [
//     { id: '001', name: 'tom', age: 18 },
//     { id: '002', name: 'jerry', age: 19 },
//     { id: '003', name: 'tony', age: 120 },
//   ]
//   response.send(users)
// })
/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;
